#!/bin/bash
#
#  OpenPom $Revision: 16 $
#  $HeadURL: https://openpom.googlecode.com/svn/trunk/send-order $
# 
#  Copyright 2010, Exosec
#  Licensed under GPL Version 2.
#  http://www.gnu.org/licenses/
# 
#  $Date: 2010-09-13 10:24:33 +0200 (Mon, 13 Sep 2010) $
#
#
# This bash script is just an exemple,
# you must improve it for you needs
#
# called as: "$EXEC_CMD" "$CMD_FILE" "$cmd" &
# $cmd is multilined
#

# uncomment for debug
# out=/tmp/${0##*/}.$RANDOM
# echo $0 $* > $out
# exec 2>> $out
# set -x

[ $# -ne 2 ] && exit 2

MAX_EXTCMD=10  ### MAX EXTERNAL COMMAND
SLEEP_TIME=5   ### SLEEP X SECONDS AFTER MAX_EXTCMD IS REACHED

oIFS=$IFS ; IFS=$'\n'
orders=( $(echo "$*") )
IFS=$oIFS

[ ! -p "${orders[0]}" ] && { echo "${orders[0]} is not a named pipe" ; exit 2 ; }

c=0
for ((i=1; i<="${#orders[@]}"; i++)) ; do
  if [ $c -gt 0 -a $(($c%$MAX_EXTCMD)) -eq 0 ] ; then
      sleep $SLEEP_TIME
  fi
  cat > "${orders[0]}" <<< "${orders[$i]}"
  c=$((c+1))
done
exit $?

